package com.wangsd.web.controller.account;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangsd.web.entity.account.Income;
import com.wangsd.web.service.account.IncomeService;
import com.wangsd.web.utils.ErrorTips;
import com.wangsd.web.utils.PageUtils;
import com.wangsd.web.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/account/income")
@Api(tags = "入账记录操作接口")
public class IncomeController {

    @Resource
    private IncomeService incomeService;

    /**
     * 记账
     * @param income 收入信息对象
     */
    @PostMapping("/keep")
    @ApiOperation("记账")
    public R keep(@ApiParam(name = "income", value = "收入信息对象", type = "Income") Income income) {
        if (income != null && incomeService.save(income)) {
            return R.success();
        }
        return R.error(ErrorTips.UN_SAVE);
    }

    /**
     * 修改入账信息
     * @param income 入账信息对象
     */
    @PutMapping("/modify")
    @ApiOperation("修改入账信息")
    public R modify(@ApiParam(name = "income", value = "收入信息对象", type = "Income") Income income) {
        if (income != null && incomeService.modify(income)) {
            return R.success();
        }
        return R.error(ErrorTips.UN_UPDATE);
    }

    /**
     * 删除账目
     * @param incomeId 账目Id
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除入账信息")
    public R delete(@ApiParam(name = "income", value = "收入信息对象", type = "Income") Integer incomeId) {
        if (incomeId != null && incomeService.delete(incomeId)) {
            return R.success();
        }
        return R.error(ErrorTips.UN_DELETE);
    }

    /**
     * 根据主键查询记录
     * @param incomeId 账目Id
     */
    @GetMapping("/no")
    @ApiOperation("通过主键Id查询入账信息")
    public void getById(@ApiParam(name = "incomeId", value = "入账Id", type = "Integer") Integer incomeId) {

    }

    /**
     * 通过用户Id查询入账记录
     * @param userId 用户Id
     */
    @GetMapping("/all/no")
    @ApiOperation("通过用户Id查询用户的所有入账信息")
    public R getAllByUserId(@ApiParam(name = "userId", value = "用户Id", type = "Integer") @RequestParam(value = "userId", defaultValue = "1") Integer userId) {
        Income income = incomeService.getById(userId);
        return income != null ? R.success().data(income) : R.error(ErrorTips.NO_SUCH_DATA);
    }

    /**
     * 根据页码进行分页查询
     * @param pageNum 页码
     * @param userId 用户Id
     */
    @GetMapping("/page")
    @ApiOperation("通过用户Id对用户入账信息进行分页查询")
    public R getByPageNo(@ApiParam(name = "pageNum", value = "页码", type = "Integer") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                         @ApiParam(name = "userId", value = "用户Id", type = "Integer") Integer userId) {
        if (userId != null) {
            List<Income> incomeList = incomeService.getTen(userId, new Page<Income>(pageNum, PageUtils.getPageSize()));
            return incomeList != null ? R.success().data(incomeList) : R.error(ErrorTips.NO_SUCH_DATA);
        }
        return R.error(ErrorTips.UN_KNOW_COLUMN);
    }
}
